<?php
/**
 * Created by PhpStorm.
 * User: wuxiaofei
 * Date: 2018/5/25
 * Time: 9:42
 */
namespace V1;

class PlayShow extends \ResourceClass
{
    public $isActive = true;
    public $remark = '竞猜首页';

    public function get(array $req, array $post, &$result){
        if (!isset($req["uid"]) || !$req['uid']) {
            return "uid is not null";
        }
        $checkRes = $this->autoCheck($req['uid']);
        if($checkRes['code'] !== 200){
            return $checkRes['message'];
        }
        $uid = $req['uid'];

        //球队
        $redis_instance = \Util\Redis::getInstance();
        $player_list = $redis_instance->get('ziyan_player_list');
        $player_list = json_decode($player_list,true);

        //竞猜选项
        $setting_list = $redis_instance->get('ziyan_setting_list');
        $setting_list = json_decode($setting_list,true);

        //赛程信息
        $showing_list = $redis_instance->get('ziyan_showing_list');
        $showing_list = json_decode($showing_list,true);

        $result = array(
            'showing_list' => array(),
            'setting_item' => array(),
            'setting_list' => array(),
            'all_chance' => 1,
            'share_time' => 0,
            'show_sort' => 0,
            'no_play' => 0,
            'rule_txt' => ''
        );

        //检查用户剩余竞猜机会
        $thisDate = date('Y-m-d');
        $activeNum = \Biz\Play\Active::getTotal('uid='.$uid.' and put_date="'.$thisDate.'"');
        $shareNum = \Biz\SharingLog::getShareByUidAndTime($uid,$thisDate);
        $shareTime = (int)$shareNum[0]['num']??0;
        $result['share_time'] = $shareTime;
        $shareTime = ($shareTime>=\Util\Config::get('max_chance_limit'))?(\Util\Config::get('max_chance_limit')-1):$shareTime;
        $result['all_chance'] = (($shareTime + 1) - $activeNum)>0?(($shareTime + 1) - $activeNum):0;

        //赛事信息
        $thisTime = date('Y-m-d H:i:s');
        //$thisTime = '2018-06-15 10:00:00';
        $list_result = array();
        foreach ($showing_list as $key => $val) {
            if($val['open_time'] < $thisTime && $val['stop_time'] > $thisTime && $val['status'] == \Biz\Play\Showings::STATUS_WAIT){
                $list_result[$key] = $val;
            }
        }
        if($list_result){
            $show_sort = 0;//下一场没有竞猜的比赛
            $play_time = 0;//是否有竞猜
            foreach ($list_result as $item){
                $_data = array(
                    'id' => $item['id'],
                    'player_a_name' => $player_list[$item['player_a']]['player_name'],
                    'player_a_icon' => $player_list[$item['player_a']]['icon_url'],
                    'player_b_name' => $player_list[$item['player_b']]['player_name'],
                    'player_b_icon' => $player_list[$item['player_b']]['icon_url'],
                    'stop_time' => date('m-d H:i',strtotime($item['stop_time'])),
                    'play_number' => $item['play_number'],
                    'title' => '第 '.'1'.' 场比赛',
                    'type_a' => array('id'=>\Biz\Play\Settings::TYPE_A,'name'=>'胜负平','target_id'=>0,'target_name'=>'待选'),
                    'type_b' => array('id'=>\Biz\Play\Settings::TYPE_B,'name'=>'全半场','target_id'=>0,'target_name'=>'待选'),
                    'type_c' => array('id'=>\Biz\Play\Settings::TYPE_C,'name'=>'比分','target_id'=>0,'target_name'=>'待选'),
                    'is_put' => 0
                );
                //处理用户的赛事竞猜
                $userActive = \Biz\Play\Active::getList('uid='.$uid.' and play_show_id='.$item['id'],1,0);
                if($userActive['lists']){
                    foreach ($userActive['lists'] as $v){
                        switch ($v['play_type']){
                            case \Biz\Play\Settings::TYPE_A:
                                $_data['type_a']['target_id'] = $v['play_target_id'];
                                $_data['type_a']['target_name'] = $setting_list[$v['play_target_id']]['title'];
                                break;
                            case \Biz\Play\Settings::TYPE_B:
                                $_data['type_b']['target_id'] = $v['play_target_id'];
                                $_data['type_b']['target_name'] = $setting_list[$v['play_target_id']]['title'];
                                break;
                            case \Biz\Play\Settings::TYPE_C:
                                $_data['type_c']['target_id'] = $v['play_target_id'];
                                $_data['type_c']['target_name'] = $setting_list[$v['play_target_id']]['title'];
                                break;
                        }
                    }
                    $_data['is_put'] = 1;//是否已参与该场竞猜
                    $play_time++;
                    $show_sort++;
                }else{
                    $result['show_sort'] = $show_sort;
                }

                $result['showing_list'][] = $_data;
            }
            if($play_time === count($list_result)){
                $result['no_play'] = 1;
            }
            $result['show_sort'] = $result['show_sort']?$result['show_sort']:$show_sort;
        }
    }
}